package com.ph.controller;

import com.ph.pojo.DeviceType;
import com.ph.pojo.Result;
import com.ph.service.DeviceTypeService;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("type")
public class DeviceTypeController {
    @Autowired
    private DeviceTypeService deviceTypeService;

    @RequiresUser
    @GetMapping
    public Result findAll(){
        List<DeviceType> deviceTypes = deviceTypeService.list();
        return Result.success(deviceTypes);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        DeviceType deviceType = deviceTypeService.getById(id);
        if(deviceType == null){
            return Result.failed("设备类型不存在");
        }
        return Result.success(deviceType);
    }

    @PostMapping
    public Result add(@RequestBody DeviceType deviceType){
        deviceType.setCreateId(1);
        deviceTypeService.save(deviceType);
        return Result.success(deviceType);
    }
    @PutMapping
    public Result update(@RequestBody DeviceType deviceType){
        if( deviceTypeService.updateById(deviceType)){
            return Result.success("修改成功");
        }else {
            return Result.success("修改失败");
        }
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        if(deviceTypeService.removeById(id)){
            return Result.success("删除成功");
        }else{
            return Result.failed("删除失败");
        }
    }


}
